package android.slc.appbase.ui.utils.sticky;

import android.app.Activity;
import android.slc.adapter.CommonlySwipeRecycler;
import android.slc.appbase.R;
import android.slc.sticky.OnStickyChangeListener;
import android.slc.sticky.StickyHeadContainer;
import android.slc.sticky.StickyItemDecoration;
import android.view.View;

import androidx.annotation.IdRes;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemChildLongClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * StickyItemDecoration兼容类，用于快速使用BaseQuickAdapter
 *
 * @author slc
 * @date 2019/10/21 16:49
 */
public class StickyItemDecorationCompat {
    private StickyItemDecoration mStickyItemDecoration;
    private StickyHeadContainer mStickyHeadContainer;
    private RecyclerView.Adapter mAdapter;
    private OnStickyChangeListener mOnStickyChangeListener;
    private OnItemChildClickListener mOnItemChildClickListener;
    private OnItemChildLongClickListener mOnItemChildLongClickListener;
    private OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener;
    private StickyHeadContainer.DataCallback mDataCallback;
    private int mPos;
    private List<Integer> mClickIdList;
    private List<Integer> mLongClickIdList;

    protected StickyItemDecorationCompat(StickyItemDecoration stickyItemDecoration, StickyHeadContainer stickyHeadContainer, RecyclerView.Adapter adapter, StickyHeadContainer.DataCallback dataCallback) {
        this.mStickyItemDecoration = stickyItemDecoration;
        this.mStickyHeadContainer = stickyHeadContainer;
        this.mAdapter = adapter;
        this.mDataCallback = dataCallback;
    }

    public void setOnStickyChangeListener(OnStickyChangeListener onStickyChangeListener) {
        this.mOnStickyChangeListener = onStickyChangeListener;
    }

    public void setOnItemChildClickListener(OnItemChildClickListener onItemChildClickListener) {
        this.mOnItemChildClickListener = onItemChildClickListener;
    }

    public void setOnItemChildLongClickListener(OnItemChildLongClickListener onItemChildLongClickListener) {
        this.mOnItemChildLongClickListener = onItemChildLongClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.mOnItemLongClickListener = onItemLongClickListener;
    }

    public void setClickIdList(@IdRes List<Integer> clickIdList) {
        this.mClickIdList = clickIdList;

    }

    public void setLongClickIdList(@IdRes List<Integer> longClickIdList) {
        this.mLongClickIdList = longClickIdList;
    }

    public StickyItemDecoration getStickyItemDecoration() {
        return mStickyItemDecoration;
    }

    public StickyHeadContainer getStickyHeadContainer() {
        return mStickyHeadContainer;
    }

    private StickyItemDecorationCompat create() {
        mStickyHeadContainer.setDataCallback(pos -> {
            StickyItemDecorationCompat.this.mPos = pos;
            mDataCallback.onDataChange(pos);
        });
        mStickyItemDecoration.setOnStickyChangeListener(new OnStickyChangeListener() {
            @Override
            public void onScrollable(int offset) {
                mStickyHeadContainer.scrollChild(offset);
                mStickyHeadContainer.setVisibility(View.VISIBLE);
                if (StickyItemDecorationCompat.this.mOnStickyChangeListener != null) {
                    StickyItemDecorationCompat.this.mOnStickyChangeListener.onScrollable(offset);
                }
            }

            @Override
            public void onInVisible() {
                mStickyHeadContainer.reset();
                mStickyHeadContainer.setVisibility(View.INVISIBLE);
                if (StickyItemDecorationCompat.this.mOnStickyChangeListener != null) {
                    StickyItemDecorationCompat.this.mOnStickyChangeListener.onInVisible();
                }
            }
        });
        BaseQuickAdapter baseQuickAdapter = mAdapter instanceof BaseQuickAdapter ? (BaseQuickAdapter) mAdapter : null;
        if (this.mOnItemClickListener != null) {
            mStickyHeadContainer.getChildAt(0).setOnClickListener(v -> StickyItemDecorationCompat.this.mOnItemClickListener.onItemClick(baseQuickAdapter, v, StickyItemDecorationCompat.this.mPos));
        }
        if (this.mOnItemChildLongClickListener != null) {
            mStickyHeadContainer.getChildAt(0).setOnLongClickListener(v -> StickyItemDecorationCompat.this.mOnItemLongClickListener.onItemLongClick(baseQuickAdapter, v, StickyItemDecorationCompat.this.mPos));
        }
        if (this.mOnItemChildClickListener != null) {
            for (Integer integer : mClickIdList) {
                mStickyHeadContainer.findViewById(integer).setOnClickListener(v -> mOnItemChildClickListener.onItemChildClick(baseQuickAdapter, v, StickyItemDecorationCompat.this.mPos));
            }
        }
        if (this.mOnItemChildLongClickListener != null) {
            for (Integer integer : mLongClickIdList) {
                mStickyHeadContainer.findViewById(integer).setOnLongClickListener(v -> mOnItemChildLongClickListener.onItemChildLongClick(baseQuickAdapter, v, StickyItemDecorationCompat.this.mPos));
            }
        }
        return this;
    }

    public static class Builder {
        private int mStickyId = R.id.shc;
        private int mItemType;
        private OnStickyChangeListener mOnStickyChangeListener;
        private RecyclerView mRecyclerView;
        private OnItemChildClickListener mOnItemChildClickListener;
        private OnItemChildLongClickListener mOnItemChildLongClickListener;
        private OnItemClickListener mOnItemClickListener;
        private OnItemLongClickListener mOnItemLongClickListener;
        private List<Integer> mClickIdList = new ArrayList<>();
        private List<Integer> mLongClickIdList = new ArrayList<>();

        public Builder(int itemType) {
            this.mItemType = itemType;
        }

        public Builder setStickyId(int stickyId) {
            this.mStickyId = stickyId;
            return this;
        }

        public void setOnStickyChangeListener(OnStickyChangeListener onStickyChangeListener) {
            this.mOnStickyChangeListener = onStickyChangeListener;
        }

        public Builder setOnItemChildClickListener(OnItemChildClickListener onItemChildClickListener) {
            this.mOnItemChildClickListener = onItemChildClickListener;
            return this;
        }

        public Builder setOnItemChildLongClickListener(OnItemChildLongClickListener onItemChildLongClickListener) {
            this.mOnItemChildLongClickListener = onItemChildLongClickListener;
            return this;
        }

        public Builder setOnItemClickListener(OnItemClickListener onItemClickListener) {
            this.mOnItemClickListener = onItemClickListener;
            return this;
        }

        public Builder setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
            this.mOnItemLongClickListener = onItemLongClickListener;
            return this;
        }

        public void addClickIdList(@IdRes List<Integer> clickIdList) {
            this.mClickIdList.addAll(clickIdList);
        }

        public void addLongClickIdList(@IdRes List<Integer> longClickIdList) {
            this.mLongClickIdList.addAll(longClickIdList);
        }

        public void addClickIds(@IdRes Integer... ids) {
            addClickIdList(Arrays.asList(ids));
        }

        public void addLongClickIds(@IdRes Integer... ids) {
            addLongClickIdList(Arrays.asList(ids));
        }

        public StickyItemDecorationCompat build(CommonlySwipeRecycler swipeRecycler, StickyHeadContainer.DataCallback dataCallback) {
            return build(swipeRecycler.getRecyclerView(), dataCallback);
        }

        public StickyItemDecorationCompat build(View contentView, CommonlySwipeRecycler swipeRecycler, StickyHeadContainer.DataCallback dataCallback) {
            return build(contentView, swipeRecycler.getRecyclerView(), dataCallback);
        }

        public StickyItemDecorationCompat build(RecyclerView recyclerView, StickyHeadContainer.DataCallback dataCallback) {
            return build(((Activity) recyclerView.getContext()).getWindow().getDecorView(), recyclerView, dataCallback);
        }

        public StickyItemDecorationCompat build(View contentView, RecyclerView recyclerView, StickyHeadContainer.DataCallback dataCallback) {
            this.mRecyclerView = recyclerView;
            StickyItemDecorationCompat stickyItemDecorationCompat = build(contentView, dataCallback);
            recyclerView.addItemDecoration(stickyItemDecorationCompat.getStickyItemDecoration());
            return stickyItemDecorationCompat;
        }

        public StickyItemDecorationCompat build(Activity activity, StickyHeadContainer.DataCallback dataCallback) {
            return build(activity.getWindow().getDecorView(), dataCallback);
        }

        public StickyItemDecorationCompat build(View contentView, StickyHeadContainer.DataCallback dataCallback) {
            StickyHeadContainer stickyHeadContainer = contentView.findViewById(this.mStickyId);
            StickyItemDecorationCompat stickyItemDecorationCompat = new StickyItemDecorationCompat(new StickyItemDecoration(stickyHeadContainer, this.mItemType), stickyHeadContainer, this.mRecyclerView != null ? this.mRecyclerView.getAdapter() : null, dataCallback);
            stickyItemDecorationCompat.setOnStickyChangeListener(this.mOnStickyChangeListener);
            stickyItemDecorationCompat.setOnItemChildClickListener(this.mOnItemChildClickListener);
            stickyItemDecorationCompat.setOnItemChildLongClickListener(this.mOnItemChildLongClickListener);
            stickyItemDecorationCompat.setOnItemClickListener(this.mOnItemClickListener);
            stickyItemDecorationCompat.setOnItemLongClickListener(this.mOnItemLongClickListener);
            stickyItemDecorationCompat.setClickIdList(this.mClickIdList);
            stickyItemDecorationCompat.setLongClickIdList(this.mLongClickIdList);
            return stickyItemDecorationCompat.create();
        }
    }
}
